home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’89 / files / fl.c < prev    next >
C/C++ Source or Header  |  1989-06-11  |  2KB  |  111 lines

  1. #include <stdio.h>
  2. #include <Files.h>
  3. #include <OSUtils.h>
  4. #include <Packages.h>
  5.  
  6. typedef struct finfo {
  7.     long    fdate;
  8.     short    findex;
  9.     } finfo;
  10.  
  11. main(argc, argv)
  12. short argc;
  13. char * argv[];
  14. {
  15. VolumeParam vp;
  16. FileParam fp;
  17. register int count, i;
  18. OSErr rc;
  19. finfo flist[256];
  20. unsigned char namebuff[64];
  21. int mycompare();
  22. char datestr[256];
  23. char timestr[256];
  24. int maxcount;
  25. char typestr[5], creatstr[5];
  26.  
  27. typestr[4] = creatstr[4] = 0;
  28. maxcount = 256;
  29. if (argc > 1) {
  30.     maxcount = atoi(argv[1]);
  31.     if ((maxcount < 1) || (maxcount > 256)) maxcount = 256;
  32.     }
  33.  
  34. vp.ioCompletion = 0;
  35. vp.ioNamePtr = 0;
  36. rc = PBGetVol(&vp, 0);
  37. if (rc != 0) {
  38.     printf("Error %d from PBGetVol.\n", rc);
  39.     return;
  40.     }
  41. count = 0;
  42. fp.ioCompletion = 0;
  43. fp.ioNamePtr = 0;
  44. fp.ioVRefNum = vp.ioVRefNum;
  45. fp.ioFVersNum = 0;
  46. fp.ioFDirIndex = count+1;
  47. rc = PBGetFInfo(&fp);
  48. while ((rc == 0) && (count < 256)) {
  49.     (flist[count]).findex = count+1;
  50.     (flist[count]).fdate = fp.ioFlMdDat;
  51.     count++;
  52.     fp.ioCompletion = 0;
  53.     fp.ioNamePtr = 0;
  54.     fp.ioVRefNum = vp.ioVRefNum;
  55.     fp.ioFVersNum = 0;
  56.     fp.ioFDirIndex = count+1;
  57.     rc = PBGetFInfo(&fp);
  58.     }
  59. if (count > 1) {
  60.     qsort(flist, count, sizeof(finfo), mycompare);
  61.     }
  62. if (count == 1) printf("1 file:\n");
  63. else if (count > maxcount) {
  64.     if (maxcount == 1) {
  65.         printf("%d file of %d:\n", maxcount, count);
  66.         }
  67.     else {
  68.         printf("%d files of %d:\n", maxcount, count);
  69.         }
  70.     count = maxcount;
  71.     }
  72.     else printf("%d files:\n", count);
  73. if (count == 0) return;
  74. for (i=0; i < count; i++) {
  75.     fp.ioCompletion = 0;
  76.     fp.ioNamePtr = namebuff;
  77.     fp.ioVRefNum = vp.ioVRefNum;
  78.     fp.ioFVersNum = 0;
  79.     fp.ioFDirIndex = (flist[i]).findex;
  80.     rc = PBGetFInfo(&fp);
  81.     if (rc != 0) {
  82.         printf("Error %d from PBGetFInfo\n", rc);
  83.         return;
  84.         }
  85.     p2cstr(namebuff);
  86.     if (strlen(namebuff) > 16) {
  87.         namebuff[15] = '…';
  88.         namebuff[16] = 0;
  89.         }
  90.     memcpy(typestr, &(fp.ioFlFndrInfo.fdType), 4);
  91.     memcpy(creatstr, &(fp.ioFlFndrInfo.fdCreator), 4);
  92.     IUDateString(fp.ioFlMdDat, abbrevDate, datestr);
  93.     IUTimeString(fp.ioFlMdDat, 1, timestr);
  94.     timestr[strlen(timestr)-2] = tolower(timestr[strlen(timestr)-2]);
  95.     timestr[strlen(timestr)-1] = tolower(timestr[strlen(timestr)-1]);
  96.     printf("%-16s %-4s %-4s  %4ldk %4ldk  %-17s %11s\n", namebuff,
  97.         typestr, creatstr,
  98.         (fp.ioFlLgLen+1023)/1024,
  99.         (fp.ioFlRLgLen+1023)/1024,
  100.         datestr, timestr);
  101.     }
  102. }
  103.  
  104. int mycompare(elem1, elem2)
  105. finfo * elem1, * elem2;
  106. {
  107. if (elem1->fdate == elem2->fdate) return(0);
  108. else if (elem1->fdate > elem2->fdate) return(-1);
  109. else return(1);
  110. }
  111.